package com.kgc.activityprovider.listener;

import com.kgc.activityprovider.service.WorkFlowUtilService;
import com.kgc.activityprovider.util.GetBeanUtil;
import com.kgc.basic.util.ResponseMsg;
import lombok.Setter;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

/**
 * 任务监听器 (Task Listener)
 *
 */

public class MyTaskListener implements TaskListener  {

    private Logger logger = LoggerFactory.getLogger(getClass());


    private Expression status;
    private Expression className;
    private Expression email;
    private Expression roleName;



    @Override
    public void notify(DelegateTask delegateTask) {
        logger.debug("MyTaskListener is start"+delegateTask.getName());
        //参数
        String code = (String)delegateTask.getVariable("code");
        Integer checkManId = (Integer) delegateTask.getVariable("checkManId");
        String checkManName = (String) delegateTask.getVariable("checkManName");
        String checkComment = (String) delegateTask.getVariable("checkComment");
        String classNameStr = className.toString();
        int statusInt = Integer.parseInt(status.toString());

        DelegateExecution execution = delegateTask.getExecution();
        //获取当前流程的key
        String key = execution.getProcessInstanceBusinessKey();
        WorkFlowUtilService workFlowUtilService = (WorkFlowUtilService) GetBeanUtil.getBean(classNameStr);
        //更改状态
        workFlowUtilService.changeStatus(key,statusInt);
        workFlowUtilService.insertCheck(code,checkManId,checkComment);

        if(email!=null){
            String emailStr = email.getExpressionText();
            String roleNameStr = roleName.getExpressionText();
            try {
                workFlowUtilService.sendEmail(code,emailStr);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
//            JavaMailSender javaMailSender = (JavaMailSender)GetBeanUtil.getBean("javaMailSender");
//
//            ResponseMsg msg = new ResponseMsg();
//            msg.setCode("200");
//
//            // 发送邮件（极简内容）
//            SimpleMailMessage message = new SimpleMailMessage();
//            message.setFrom("1805845059@qq.com");
//            message.setTo(emailStr);
//            message.setSubject(roleNameStr+"这是发给你的测试邮件");
//            message.setText(roleNameStr+"请去审批");
//            if (javaMailSender != null) {
//                javaMailSender.send(message);
//            }
//            logger.debug("UserController sendEmail email:"+email);
//            logger.debug("UserController sendEmail message:"+message);
//            logger.debug("UserController sendEmail msg:"+msg);
        }

    }

    public void onEvent(ActivitiEvent activitiEvent) {
        ActivitiEventType type = activitiEvent.getType();
        System.out.println(type);
    }


}
